package com.dropTables.TakingNote;


import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;

import java.util.List;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class LocationSelectMap extends MapActivity 
{
	private MapView mymap;
	private MapController mapcon;
	
	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.select_map);
		
		Bundle extras = getIntent().getExtras();
		String lst[] = null;
		
		if(extras != null)
		{
			String input = extras.getString(LocationEditActivity.KEY_LIST_STRING);		//Get the results from the address search the user input
			lst = input.split(":");														//Each entry is separated by colons and each entry has 2 parts, also separated by colons
		}
		
		mymap = (MapView) findViewById(R.id.location_edit_mapview);				//Get the map to draw
		mymap.setBuiltInZoomControls(true);
		mapcon = mymap.getController();
		mapcon.setZoom(2);
		
		List<Overlay> mapOverlays = mymap.getOverlays();							//Get the overlays
		Drawable draw = this.getResources().getDrawable(R.drawable.icon_map_point);
		MapOverlay locOverlay = new MapOverlay(draw, this);							//New locOverlay for the addresses
		
		
		for(int i = 0; i < lst.length-1; i+=2)
		{
			String pos[] = lst[i+1].split(",");										//Separate latitude and longitude
			double val[] = new double[2];
			val[0] = Double.valueOf(pos[0]);										//Convert to double
			val[1] = Double.valueOf(pos[1]);
			
			int lat = (int)(val[0]*1E6);											//GeoPoint = GPS coordinates *10^6
			int lng = (int)(val[1]*1E6);
			
	
			GeoPoint point = new GeoPoint(lat, lng);
			OverlayItem item = new OverlayItem(point, lst[i], lst[i+1]);
			locOverlay.addOverlay(item);
		}
		
		mapOverlays.add(locOverlay);						//Add the overlay to the map
		
	}
	
	
	/*
	 * @param name   the selected point's name
	 * @param loc    the selected points GPS coordinates in string form
	 * 
	 * @return void
	 * 
	 * Called once a location is selected
	 */
	public void locSelected(String name, String loc)
	{
		Intent rIntent = new Intent();
		Bundle extras = new Bundle();
		extras.putString(LocationEditActivity.KEY_SELECTED_NAME, name);			//Return name and GPS coord in string form
		extras.putString(LocationEditActivity.KEY_SELECTED_LOC, loc);
		
		rIntent.putExtras(extras);
		setResult(RESULT_OK, rIntent);
		finish();
	}
	
	@Override
	protected boolean isRouteDisplayed() 
	{
		return false;
	}

}
